package TestSparkConversionOperator;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import scala.Tuple2;

import java.util.Arrays;
import java.util.List;

public class SparkCartesian {
    /**
     * 在spark中，cartesian算子的作用就是求两个RDD的笛卡尔积，得到一个新的RDD。
     */
    public static void main(String[] args) {
        SparkConf sparkConf = new SparkConf().setAppName("Operator3").setMaster("local");
        JavaSparkContext sc = new JavaSparkContext(sparkConf);

        JavaRDD<String> rdd1 = sc.parallelize(Arrays.asList("a1", "a2"), 2);
        JavaRDD<String> rdd2 = sc.parallelize(Arrays.asList("b1", "b2", "b3"), 2);

        JavaPairRDD<String, String> cartesian = rdd1.cartesian(rdd2);
        List<Tuple2<String, String>> res = cartesian.collect();

        for (Tuple2<String, String> tuple2 : res){
            System.out.println(tuple2);
        }
    }
}
